package com.tencent.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tencent.entity.CheckGroup;
import com.tencent.entity.Pagination;
import com.tencent.entity.SetMeal;
import com.tencent.mapper.SetMealMapper;
import com.tencent.service.SelMealService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.font.FontRunIterator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SelMealServiceImpl implements SelMealService {
    @Autowired
    private SetMealMapper setMealMapper;



    @Override
    public List<SetMeal> findAll() {

        return setMealMapper.findAll();
    }



    @Override
    public void deleteByCode(String id){
        int i = setMealMapper.deleteByCode(id);

    }

    @Override
    public PageInfo<SetMeal> findByCode(Pagination pagination ) {
        PageHelper.startPage(pagination.getCurrentPage(),pagination.getPageSize());
        List<SetMeal> byCode = setMealMapper.findByCode(pagination.getQueryString());
        return  new PageInfo<>(byCode);
    }

    @Override
    public int updateSetmeal(SetMeal setMeal) {
        return setMealMapper.updateSetmeal(setMeal);
    }

    @Override
    public int deleteByid(String id) {
        int i = setMealMapper.deleteByid(id);

        return i;
    }

    @Override
    public SetMeal findById(Integer id) {
        return setMealMapper.findById(id);
    }

    @Override
    public List<CheckGroup> findaddcheckGroup() {
        return setMealMapper.findaddcheckGroup();
    }


    @Override
    public int insertsetmeal(Integer setmealid, Integer checkgroupid) {
        return 1;
    }



    @Override
    public  int add(SetMeal setMeal ,Integer[] checkgroupIds) {
        int i = 0;
        int add = setMealMapper.add(setMeal);
        System.out.println(add);

        if(checkgroupIds != null && checkgroupIds.length > 0){
            //绑定套餐和检查组的多对多关系
            i = setSetmealAndCheckGroup(setMeal.getId(), checkgroupIds);
            System.out.println(i);
        }
        return i;
    }

    public int setSetmealAndCheckGroup(Integer setmealid, Integer[] checkgroupIds) {
        int i = 0;
        for (Integer checkgroupid : checkgroupIds) {
            Map<String,Integer> map = new HashMap<>();
            map.put("setmeal_id",setmealid);
            map.put("checkgroup_id",checkgroupid);
            i = setMealMapper.insertsetmeal(map);
        }
        return i;
    }

    @Override
    public List<CheckGroup> findcheckgroupbyid(String id) {

        return setMealMapper.findcheckgroupbyid(id);
    }
}
